package com.bq.gametrade.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;

import com.bq.gametrade.R;
import com.bq.gametrade.adapter.FragmentAdapter;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.view.MyRadioButton;
import com.bq.gametrade.view.MyRadioGroup;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {

    private long firstime = 0;

    private ViewPager viewPager;
    private MyRadioGroup rg_bar;
    private Context context;
    public static int previousCheckedId = FragmentAdapter.TAB_HOME;
    private final int LOGINREQUEST = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        findViews();
        serListeners();
        init(getIntent());
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            checkPermission();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        init(intent);
    }

    private void init(Intent intent) {
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        boolean isLogin= intent.getBooleanExtra("isLogin", false);
        if (isLogin) {
            viewPager.setCurrentItem(FragmentAdapter.TAB_ACCOUNT);
            previousCheckedId = FragmentAdapter.TAB_ACCOUNT;
            setCurrentPage(FragmentAdapter.TAB_ACCOUNT);
        } else {
            setCurrentPage(FragmentAdapter.TAB_HOME);
        }
    }


    /**
     * 当程序退出的时候把TaskExecutor关掉
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    private void findViews() {
        viewPager = serchViewById(R.id.viewpager);
        rg_bar = serchViewById(R.id.rg_bar);
        setButtonItems();
    }

    private void setButtonItems() {
        ArrayList<MyRadioButton> radioButtons = new ArrayList<>();
        radioButtons.add(
                new MyRadioButton(
                        R.mipmap.foot_home_normal,
                        R.mipmap.foot_home_press,
                        getString(R.string.bar_home),
                        FragmentAdapter.TAB_HOME,
                        this));
        radioButtons.add(
                new MyRadioButton(
                        R.mipmap.foot_trading_normal,
                        R.mipmap.foot_trading_press,
                        getString(R.string.bar_trading),
                        FragmentAdapter.TAB_TRADING,
                        this));
        radioButtons.add(
                new MyRadioButton(
                        R.mipmap.foot_friend_normal,
                        R.mipmap.foot_friend_press,
                        getString(R.string.bar_friend),
                        FragmentAdapter.TAB_FRIEND,
                        this));
        radioButtons.add(
                new MyRadioButton(
                        R.mipmap.foot_myaccount_normal,
                        R.mipmap.foot_myaccount_press,
                        getString(R.string.bar_account),
                        FragmentAdapter.TAB_ACCOUNT,
                        this));

        rg_bar.setRadioButtons(radioButtons);
    }



    public void setCurrentPage(int PageId) {
        viewPager.setCurrentItem(PageId);
        rg_bar.setRadioButtonChecked(PageId);
    }

    private void serListeners() {
        rg_bar.setOnItemChangedListener(new MyRadioGroup.OnItemChangedListener() {
            @Override
            public void OnItemChanged(int checkedId) {
                if (gotoLogin(checkedId)) return;
            }
        });
    }

    private boolean gotoLogin(int i) {
        if (i == FragmentAdapter.TAB_ACCOUNT) {
            if (!application.isLogin()) {
                startActivity(new Intent(context, LoginActivity.class));
                return true;
            }
        }
        viewPager.setCurrentItem(i);
        previousCheckedId = i;
        return false;
    }

    /**
     * 用户第一次 点击back键时，提示用户‘再按一次返回键退出’
     * 第二次点击back键时将推出程序。
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long secondtime = System.currentTimeMillis();
            if (secondtime - firstime > 3000) {
                toastShow("再按一次返回键退出");
                firstime = System.currentTimeMillis();
                return true;
            } else {
                finish();
//                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}


